const isClipboardSupported = () => {
  return (
    !!navigator.clipboard && typeof navigator.clipboard.writeText === "function"
  );
};

const fallbackCopyText = (text) => {
  const textarea = document.createElement("textarea");
  textarea.value = text;
  textarea.style.position = "absolute";
  textarea.style.opacity = "0";
  document.body.appendChild(textarea);
  textarea.select();
  document.execCommand("copy");
  textarea.remove();
};

const useClipboard = () => {

  const copyText = (text) => {
    if (isClipboardSupported()) {
      navigator.clipboard
        .writeText(text)
        .then(() => {
          
        })
        .catch((error) => {
          console.error("Failed to copy text: ", error);
        });
    } else {
      fallbackCopyText(text);
    }
  };

  return { copyText };
};

export default useClipboard;
